﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Popups
{
    public partial class ConfirmAutoClose : Form
    {
        private int delayInSeconds;

        public ConfirmAutoClose(int delayInSeconds)
        {
            InitializeComponent();
            this.delayInSeconds = delayInSeconds;
        }

        public static DialogResult ShowConfirmationDialog(string title, string message, int delayInSeconds)
        {
            ConfirmAutoClose dialog = new ConfirmAutoClose(delayInSeconds);
            dialog.Text = title;
            dialog.LabelMessage.Text = message;
            return dialog.ShowDialog();
        }

        private void ExConfirmAutoClose_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(AutoClose));
            thread.Start();
        }

        private void AutoClose()
        {
            Thread.Sleep(delayInSeconds * 1000);
            ExecuteAutoClose();
        }

        delegate void CloseCallback();

        private void ExecuteAutoClose()
        {
            if (this.InvokeRequired)
            {
                CloseCallback d = new CloseCallback(Close);
                this.Invoke(d);
            }
            else
            {
                this.Close();
            }
        }

        private void ConfirmAutoClose_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(AutoClose));
            thread.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
